package pl.ppsoft.fixlang;

import java.text.DateFormat;

import android.app.ExpandableListActivity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListAdapter;
import android.widget.ProgressBar;
import android.widget.SimpleCursorTreeAdapter;
import android.widget.TextView;

public class HistoryActivity extends ExpandableListActivity {

	private DbAdapter db;
	private SharedPreferences settings;
	private int historyIdColumnIndex;
	private Cursor historyCursor;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        db = new DbAdapter(this);
        db.open();
        settings = PreferenceManager.getDefaultSharedPreferences(this);
        
        historyCursor = db.fetchHistory();
        startManagingCursor(historyCursor);
        
        historyIdColumnIndex = historyCursor.getColumnIndexOrThrow( DbAdapter.HISTORY_ROWID );
        
        ExpandableListAdapter mAdapter = new HistExpandableListAdapter(historyCursor,
						                this,
						                R.layout.history_row,
						                R.layout.history_lessons_row,
						                new String[] {DbAdapter.HISTORY_TIME, DbAdapter.HISTORY_TOTAL, DbAdapter.HISTORY_PASSED, DbAdapter.HISTORY_QUESTIONING, DbAdapter.HISTORY_ANSWERING, DbAdapter.HISTORY_LISTENING, DbAdapter.HISTORY_WRITING}, 
						                new int[] {R.id.history_time, R.id.progress_total, R.id.progress_passed, R.id.questioning_chart, R.id.answering_chart, R.id.listening_chart, R.id.writing_chart},
						                new String[] {DbAdapter.LESSON_NAME, DbAdapter.HISTLESSON_TOTAL},
						                new int[] {R.id.hist_lesson_name, R.id.hist_lesson_total});
        
        setListAdapter(mAdapter);
	}

	@Override
	protected void onResume() {
        if ( this.settings.getBoolean(FixLangActivity.MODIFY_HISTORY_FLAG, false) ) {
            Editor editor = this.settings.edit();
            editor.remove(FixLangActivity.MODIFY_HISTORY_FLAG);
            editor.commit();
            this.historyCursor.requery();
        }
		
		super.onResume();
	}
	
    public class HistExpandableListAdapter extends SimpleCursorTreeAdapter {

        public HistExpandableListAdapter(Cursor cursor, Context context, int groupLayout,
                int childLayout, String[] groupFrom, int[] groupTo, String[] childrenFrom,
                int[] childrenTo) {
            super(context, cursor, groupLayout, groupFrom, groupTo, childLayout, childrenFrom,
                    childrenTo);
            
			final DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT);            
            final int timeColumnIndex = cursor.getColumnIndexOrThrow(DbAdapter.HISTORY_TIME);
            final int totalColumnIndex = cursor.getColumnIndexOrThrow(DbAdapter.HISTORY_TOTAL);
            final int passedColumnIndex = cursor.getColumnIndexOrThrow(DbAdapter.HISTORY_PASSED);
            final int qaColumnIndex = cursor.getColumnIndexOrThrow(DbAdapter.HISTORY_QUESTIONING);
            final int aqColumnIndex = cursor.getColumnIndexOrThrow(DbAdapter.HISTORY_ANSWERING);
            final int lColumnIndex = cursor.getColumnIndexOrThrow(DbAdapter.HISTORY_LISTENING);
            final int wColumnIndex = cursor.getColumnIndexOrThrow(DbAdapter.HISTORY_WRITING);
            
            this.setViewBinder( new ViewBinder() {
				
				@Override
				public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
					if ( cursor != historyCursor ) {
						return false;
					}
					
					if ( columnIndex == timeColumnIndex ) {
						((TextView)view).setText( dateFormat.format(cursor.getLong(columnIndex) ) ); 											
						return true;
					} else 	if ( columnIndex == totalColumnIndex ) {
						((ProgressBar)((View)view.getParent()).getTag()).setMax( cursor.getInt(columnIndex) );
						return false;
					} else 	if ( columnIndex == passedColumnIndex ) {
						((ProgressBar)((View)view.getParent()).getTag()).setProgress( cursor.getInt(columnIndex) );
						return false;
					} else 	if ( columnIndex == qaColumnIndex ) {
						view.setVisibility( cursor.getInt(columnIndex) > 0?View.VISIBLE:View.GONE );
						return true;
					} else 	if ( columnIndex == aqColumnIndex ) {
						view.setVisibility( cursor.getInt(columnIndex) > 0?View.VISIBLE:View.GONE );
						return true;
					} else 	if ( columnIndex == lColumnIndex ) {
						view.setVisibility( cursor.getInt(columnIndex) > 0?View.VISIBLE:View.GONE );
						return true;
					} else 	if ( columnIndex == wColumnIndex ) {
						view.setVisibility( cursor.getInt(columnIndex) > 0?View.VISIBLE:View.GONE );
						return true;
					} else {
						return false;
					}
				}
			});
        }

        @Override
        public View newGroupView(Context context, Cursor cursor, boolean isExpanded, ViewGroup parent) {
			View v = super.newGroupView(context, cursor, isExpanded, parent);
			
			if ( v.getTag() == null ) {				
				ProgressBar progress = (ProgressBar)v.findViewById(R.id.chart_progress);
				v.setTag( progress );
			}
			return v;
        }
                
        @Override
        protected Cursor getChildrenCursor(Cursor groupCursor) {        	        	
            return db.fetchHistoryLessons( groupCursor.getLong(historyIdColumnIndex) );
        }

    }
	
}
